
package tiling;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.tiled.TiledMap;

/**
 *
 * @author ant
 * 
 * This are the "puzzle pieces" which will be combined to create the full map
 * of a single loading area / floor. Each object encapsulates a TilEd map 
 * plus wrapper information. 
 */
public class TiledSubMap
{
    public static final int tileSize = 32;
    
    
    public TiledSubMap(String tilemapName, int xPosTilespace, int yPosTilespace) throws SlickException
    {
        tmap = new TiledMap(tilemapName, "assets/maps");
        tileRect = new Rectangle(xPosTilespace, yPosTilespace, tmap.getWidth(), tmap.getHeight());
        euclideanRect = new Rectangle(tileRect.getX()*tileSize,
                                      tileRect.getY()*tileSize,
                                      tileRect.getWidth()*tileSize,
                                      tileRect.getHeight()*tileSize);
        
        //for now just load the first object group as walls
        //can be generalized for multiple object groups later
        
        
         for(int i=0;i<tmap.getObjectGroupCount(); ++i)
         {
            for(int j=0, count = tmap.getObjectCount(i); j < count; ++j)
            {
                int x      = tmap.getObjectX(i, j);
                int y      = tmap.getObjectY(i, j);

                switch(tmap.getObjectName(i, j))
                {
                    case "wall":
                        int height = tmap.getObjectHeight(i, j);
                        int width  = tmap.getObjectWidth(i, j);

                        walls.add(new Rectangle(x, y, width, height));
                        break;
                    case "player_start":
                        player_start = new Vector2f(x,y);
                        break;
                    case "enemy1":
                        enemy_start.add(new Vector2f(x,y));
                        break;
                    default:
                         
                }
                
            }
        }
//        tmap.getLayerIndex(tilemapName);
    }
            
    TiledMap tmap;
    
    //for now objects on the map are basically just walls. later this can be
    //generalized to include other kinds of static, environmental objects
    //switches, treasure chests, spawn points
    public ArrayList<Rectangle> walls = new ArrayList<>();
        
    //the java spec guarentees ieee754, therefore even though these are floats
    //they will have integer precision up to 2^21 (two million).
    private Rectangle tileRect;    
    private Rectangle euclideanRect;
    public Vector2f player_start;
    public ArrayList<Vector2f> enemy_start = new ArrayList<>();

    public void renderSpace(Rectangle euclideanToRender)
    {
        if(!euclideanToRender.intersects(euclideanRect))
            return;
        
        //find position of ul corner based on offset in render rect
        int posX = (int) (euclideanRect.getMinX() - euclideanToRender.getMinX());
        int posY = (int) (euclideanRect.getMinY() - euclideanToRender.getMinY());
        
        tmap.render(posX, posY);
    }
}
